package com.gxgeek.service;

import com.google.common.collect.Lists;
import com.gxgeek.dto.req.UserReq;
import com.gxgeek.entity.DataRecordEntity;
import com.gxgeek.entity.mongo.UserBasicEntity;
import com.gxgeek.enums.RiskDataTypeEnum;
import com.gxgeek.repository.DataRecordRepository;
import com.gxgeek.repository.mongo.UserAuthMongoRepository;
import com.gxgeek.repository.mongo.UserBasicMongoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.joining;

/**
 * @author gaoxing
 * @create 2018-06-04 下午3:40
 */
@Service
public class DataService {
    @Autowired
    DataRecordRepository dataRecordrepository;
    @Autowired
    UserBasicMongoRepository userBasicMongoRepository;
    @Autowired
    UserAuthMongoRepository userAuthMongoRespository;


    //获取用户信息
    public Object getLastUserData( String userId, RiskDataTypeEnum type) {
        if (type ==null){
//            throw new RuntimeException("请输入查询类型");
            return null;
        }
        DataRecordEntity snapshot = dataRecordrepository.findLastEntity(userId, type.name());
        if(snapshot == null){
            return null;
        }
        String snapshotId = snapshot.getSnapshotId();
        if (type != null && type.equals(RiskDataTypeEnum.BASIC)) {
            return userBasicMongoRepository.findById(snapshotId).get();
        }

        return userAuthMongoRespository.findById(snapshotId).get().getData();
    }


    public void storeUserInfo(UserReq reqDto) {

        //获取最近一次拉取的用户基本信息快照ID
        DataRecordEntity record = dataRecordrepository.findLastEntity(reqDto.getUserId(),RiskDataTypeEnum.BASIC.name());
        UserBasicEntity oldInfo;
        if (record != null) {
            Optional<UserBasicEntity> userBasicEntityOptional = userBasicMongoRepository.findById(record.getSnapshotId());
            if(!userBasicEntityOptional.isPresent()){
                oldInfo = new UserBasicEntity();
                oldInfo.setUserId(reqDto.getUserId());
            }
            oldInfo = userBasicEntityOptional.get();
        } else {
            oldInfo = new UserBasicEntity();
            oldInfo.setUserId(reqDto.getUserId());
        }
        List<String> changeField = Lists.newArrayList();
        if (reqDto.getBasic() != null) {
            oldInfo.setBasic(reqDto.getBasic());
        }
        if (reqDto.getEducation() != null) {
            oldInfo.setEducation(reqDto.getEducation());
            changeField.add("education");
        }
        if (reqDto.getMarriage() != null) {
            oldInfo.setMarriage(reqDto.getMarriage());
            changeField.add("marriage");
        }
        if (reqDto.getHabitancy() != null) {
            oldInfo.setHabitancy(reqDto.getHabitancy());
            changeField.add("habitancy");
        }
        if (reqDto.getCompany() != null) {
            oldInfo.setCompany(reqDto.getCompany());
            changeField.add("company");
        }
        if (reqDto.getEmergentContacts() != null) {
            oldInfo.setEmergentContacts(reqDto.getEmergentContacts());
            changeField.add("emergencyContact");
        }
        if (reqDto.getContacts() != null) {
            oldInfo.setContacts(reqDto.getContacts());
            changeField.add("contact");
        }
        String resultDescription = changeField.stream().collect(joining(","));
        // 需要mongo重新生成ID插入
        oldInfo.setId(null);
        userBasicMongoRepository.save(oldInfo);

        //数据记录入库
        DataRecordEntity result = new DataRecordEntity();
        result.setUserId(oldInfo.getUserId());
        result.setSnapshotId(oldInfo.getId());
        result.setType(RiskDataTypeEnum.BASIC.name());
        result.setCreatedAt(new Date());
//        result.setCrawlTime(new Date());
//        result.setDescription(resultDescription);
        dataRecordrepository.save(result);

    }
}
